Skip to content

feat(event_handler): enable support for custom deserializer to parse the request body #6601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

LucasCMFBraga
Copy link
Contributor

Issue number: #6600

Summary

This feature allows to pass a custom deserializer to ensure accurate handling of floating-point precision.

Changes

Added support for passing a custom deserializer instance to ApiGatewayResolver to handle JSON numeric values as Decimal, ensuring correct floating-point precision.

`
from tests.functional.utils import load_even
import json
from decimal import Decimal

app = ApiGatewayResolver(deserializer=partial(json.loads, parse_float=Decimal))
`

User experience

Please share what the user experience looks like before and after this change

`
app = ApiGatewayResolver(deserializer=partial(json.loads, parse_float=Decimal))

@app.post("/my/path")
def test_handler():
    return app.current_event.json_body

# WHEN calling the event handler
event = {}
event.update(LOAD_GW_EVENT)
event["body"] = '{"amount": 2.2999999999999998}'
event["httpMethod"] = "POST"

result = app(event, {})
# THEN process event correctly
assert result["statusCode"] == 200
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]
assert result["body"] == '{"amount":"2.2999999999999998"}'

`

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@LucasCMFBraga LucasCMFBraga requested a review from a team as a code owner May 2, 2025 17:56
@LucasCMFBraga LucasCMFBraga requested a review from anafalcao May 2, 2025 17:56
Copy link

boring-cyborg bot commented May 2, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 2, 2025
@leandrodamascena leandrodamascena changed the title feat: Enabled support for a custom deserializer to parse the request body feat(event_handler): enable support for custom deserializer to parse the request body May 12, 2025
@leandrodamascena leandrodamascena linked an issue May 12, 2025 that may be closed by this pull request
2 tasks
@github-actions github-actions bot added the feature New feature or functionality label May 12, 2025
@leandrodamascena
Copy link
Contributor

Hi @LucasCMFBraga, thanks for working on this PR! I've reviewed it and to me it makes sense for us to add these new parameter to propagate it to DataClasses, but I'm wondering if we're parsing float incorrectly and if it would make sense to make it default here: https://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/utilities/data_classes/common.py#L89

WDYT?

Copy link

codecov bot commented May 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.09%. Comparing base (818ad49) to head (cbb3027).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6601   +/-   ##
========================================
  Coverage    96.09%   96.09%           
========================================
  Files          254      254           
  Lines        12178    12179    +1     
  Branches       905      905           
========================================
+ Hits         11703    11704    +1     
  Misses         373      373           
  Partials       102      102           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucasCMFBraga
Copy link
Contributor Author

LucasCMFBraga commented May 12, 2025

WDYT

We could set Decimal as the default for parsing numeric values in JSON, but I would also allow the option to pass a custom JSON deserializer for parsing the request body. This isn’t a case of Python parsing floats incorrectly, it’s just the inherent behavior of floating-point precision.

@LucasCMFBraga LucasCMFBraga removed their assignment May 19, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label May 26, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label May 26, 2025
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation github-actions Pull requests that update Github_actions code labels May 26, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label May 26, 2025
Copy link

@leandrodamascena leandrodamascena self-requested a review May 26, 2025 18:37
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @LucasCMFBraga tudo bom? 😄

First of all sorry for the delay in reviewing this, I had to prioritize some internal demands. I made some small changes in this PR, especially renaming the name of the parameter and adding examples to make it more clear for customers.

Again, thanks a lot for bringing up this limitation and addressing this.

APPROVED.

@leandrodamascena leandrodamascena self-assigned this May 26, 2025
@leandrodamascena leandrodamascena merged commit d89f1f9 into aws-powertools:develop May 26, 2025
13 checks passed
Copy link

boring-cyborg bot commented May 26, 2025

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality github-actions Pull requests that update Github_actions code size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Enable to change the deserializer body
3 participants